package com.controller;

import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.IdWorker;
import com.service.WeixinPayService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    WeixinPayService weixinPayService;

    /**
     * 生成二维码
     * @return
     */
    @RequestMapping("/createNative.do")
    public Map createNative(){
        IdWorker idworker=new IdWorker();
        return weixinPayService.createNative(idworker.nextId()+"","1");
    }

    @RequestMapping("/queryOrder.do")
    public Result queryPayStatus(@RequestParam("out_trade_no") String out_trade_no){
        Result result=null;
        while(true){
            //调用查询接口
            Map<String,String> map = weixinPayService.queryPayStatus(out_trade_no);
            if(map==null){//出错
                result=new  Result(false, "支付出错");
                return result ;
            }
            if(map.get("trade_state").equals("SUCCESS")){//如果成功
                result=new  Result(true, "支付成功");
                break;
            }
            try {
                Thread.sleep(3000);//间隔三秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}